/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.serv.shop.entity.ShopProductCategory;
import com.jnzc.ocr.serv.shop.filter.ShopProductCategoryFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopProductCategoryMapper;
import com.jnzc.ocr.serv.shop.service.ShopProductCategoryService;
import com.jnzc.ocr.serv.shop.vo.ShopProductCategorySaveVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Service
public class ShopProductCategoryServiceImpl extends MybatisServiceImpl<ShopProductCategoryMapper, ShopProductCategory, ShopProductCategoryFilter> implements ShopProductCategoryService {

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopProductCategory> ew, ShopProductCategoryFilter filter) {
        ShopProductCategory entity = new ShopProductCategory();
        entity.setId(filter.getId());
        entity.setShopId(filter.getShopId());
        ew.setEntity(entity);
    }

    @Override
    public ShopProductCategory find(Long id, Long shopId) {
        ValidateUtils.isId(id);
        ShopProductCategoryFilter filter = new ShopProductCategoryFilter();
        filter.setId(id);
        filter.setShopId(shopId);
        return findNotNull(filter);
    }

    @Override
    @Transactional
    public void save(ShopProductCategorySaveVo param, Long shopId) {
        ValidateUtils.isId(shopId, "店铺ID");
        ValidateUtils.notEmpty(param.getName(), 20, "分类名称");

        ShopProductCategory obj;
        if(null == param.getId()) {
            obj = new ShopProductCategory();
            obj.setShopId(shopId);
            obj.setCreateTime(DateUtils.getNow());
        } else {
            obj = this.find(param.getId(), shopId);
        }
        obj.setName(param.getName());
        this.save(obj);
    }

    @Override
    public void delete(Long id, Long shopId) {
        ValidateUtils.isId(id);
        ShopProductCategoryFilter filter = new ShopProductCategoryFilter();
        filter.setId(id);
        filter.setShopId(shopId);
        super.delete(filter);
    }
}
